package com.gzw.dms.activity;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.gzw.dms.R;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.base.BaseSpinnerAdapter;
import com.gzw.dms.bean.SpinnerInfo;
import com.gzw.dms.utils.T;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by Administrator on 2016/2/29.
 */
public class SelectActivity extends BaseActivity{
    private Toolbar toolbar;

    private EditText et_num,et_chddh,et_fhkh,et_shkh,et_endress;
    private TextView tv_startTime,tv_endTime;
    private Spinner sp_sign_state;
    private Context mContext;
    private Button btn_select;
    private DatePickerDialog start_dpd,end_dpd;
    private String back;
    private Intent mIntent;
    private LinearLayout layout_sljg,layout_sate;
    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_select);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        et_num=(EditText) findViewById(R.id.et_select_num);
        et_chddh=(EditText) findViewById(R.id.et_select_chddh);
        et_fhkh=(EditText) findViewById(R.id.et_select_fhkh);
        et_shkh=(EditText) findViewById(R.id.et_select_shku);
        et_endress=(EditText) findViewById(R.id.et_select_endress);
        tv_startTime=(TextView)findViewById(R.id.tv_start_time);
        tv_endTime=(TextView)findViewById(R.id.tv_end_time);
        sp_sign_state=(Spinner)findViewById(R.id.sp_sign_state);
        btn_select=(Button)findViewById(R.id.btn_select);
        layout_sljg=(LinearLayout)findViewById(R.id.layout_sljg);
        layout_sate=(LinearLayout)findViewById(R.id.layout_sate);


    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("条件查询");
        setSupportActionBar(toolbar);
        ArrayList ls = new ArrayList();
        ls.add(new SpinnerInfo("", "全部"));
        ls.add(new SpinnerInfo("1", "已分配"));
        ls.add(new SpinnerInfo("2", "已接单"));
        ls.add(new SpinnerInfo("4", "已签收"));

        ArrayList ls1 = new ArrayList();
        ls1.add(new SpinnerInfo("", "全部"));
        ls1.add(new SpinnerInfo("1", "未签收"));
        ls1.add(new SpinnerInfo("4", "已签收"));
        mIntent = getIntent();
        back = (String) mIntent.getExtras().get("back");
        if (back.equals("2")){
            layout_sljg.setVisibility(View.GONE);
            BaseSpinnerAdapter Adapter1 = new BaseSpinnerAdapter(mContext, ls1);
            sp_sign_state.setAdapter(Adapter1);
        }else if (back.equals("0")){
            layout_sate.setVisibility(View.GONE);
            BaseSpinnerAdapter Adapter1 = new BaseSpinnerAdapter(mContext, ls1);
            sp_sign_state.setAdapter(Adapter1);
        }else if (back.equals("3")){
            layout_sljg.setVisibility(View.GONE);
            layout_sate.setVisibility(View.GONE);
            BaseSpinnerAdapter Adapter1 = new BaseSpinnerAdapter(mContext, ls1);
            sp_sign_state.setAdapter(Adapter1);
        } else if (back.equals("8")){
            layout_sate.setVisibility(View.GONE);
            BaseSpinnerAdapter Adapter1 = new BaseSpinnerAdapter(mContext, ls1);
            sp_sign_state.setAdapter(Adapter1);
        }else {
            layout_sate.setVisibility(View.GONE);
            BaseSpinnerAdapter Adapter = new BaseSpinnerAdapter(mContext, ls);
            sp_sign_state.setAdapter(Adapter);
        }
    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new myOnClick());
        btn_select.setOnClickListener(new myOnClick());
        tv_startTime.setOnClickListener(new myOnClick());
        tv_endTime.setOnClickListener(new myOnClick());

    }


    class myOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {

                case R.id.tv_start_time:
                    Calendar mCalendar = Calendar.getInstance(Locale.CHINA);
                    int year = mCalendar.get(Calendar.YEAR);
                    int month = mCalendar.get(Calendar.MONTH);
                    int day = mCalendar.get(Calendar.DAY_OF_MONTH);

                    start_dpd = new DatePickerDialog(mContext, new Datelistener(), year, month,
                            day);
                    start_dpd.show();
                    break;
                case R.id.tv_end_time:
                    Calendar mCalendar1 = Calendar.getInstance(Locale.CHINA);
                    int year1 = mCalendar1.get(Calendar.YEAR);
                    int month1 = mCalendar1.get(Calendar.MONTH);
                    int day1 = mCalendar1.get(Calendar.DAY_OF_MONTH);

                    end_dpd = new DatePickerDialog(mContext, new Datelistener1(), year1, month1,
                            day1);
                    end_dpd.show();
                    break;
                case R.id.btn_select:
                    sendBack();
                    break;

                default:
                    finish();
                    break;
            }
        }
    }


    private void sendBack(){


        if (tv_startTime.getText().toString().length()!=0){
            if(tv_endTime.getText().toString().length()==0){
                T.showShort(mContext,"请选择终止日期");
                return;
            }
        }

        if(tv_endTime.getText().toString().length()!=0){
            if(tv_startTime.getText().toString().length()==0){
                T.showShort(mContext,"请选择开始日期");
                return;
            }
        }
//        if(tv_startTime.getText().toString().length()!=0&&tv_endTime.getText().toString().length()!=0){
//            String endTime = tv_endTime.getText().toString();
//            String startTime = tv_startTime.getText().toString();
//
//            int cha = getDateDays(endTime,startTime);
//            if(cha>5) {
//                T.showShort(mContext, "日期查询间隔不能超过五天!");
//                return;
//            }
//        }
        sendData();

    }

    private void sendData(){
        SpinnerInfo spinnerInfo=(SpinnerInfo)sp_sign_state.getSelectedItem();

        Intent mIntent = new Intent();
        //把返回数据存入Intent
        mIntent.putExtra("order_num",et_num.getText().toString());
        mIntent.putExtra("shiping_order_goid",et_chddh.getText().toString());
        mIntent.putExtra("custom_name",et_fhkh.getText().toString());
        mIntent.putExtra("receipt_name", et_shkh.getText().toString());
        mIntent.putExtra("startTime", tv_startTime.getText().toString());
        mIntent.putExtra("endTime", tv_endTime.getText().toString());
        mIntent.putExtra("endress", et_endress.getText().toString());
        mIntent.putExtra("state2", spinnerInfo.getId());
        if (back.equals("1")){
            //设置返回数据
            setResult(2, mIntent);
            finish();
        }else if (back.equals("2")){
            //设置返回数据
            setResult(2, mIntent);
            finish();
        } else if (back.equals("0")){
            //设置返回数据
            setResult(2, mIntent);
            finish();
        }else if (back.equals("3")){
            //设置返回数据
            setResult(2, mIntent);
            finish();
        }else if (back.equals("8")){
            //设置返回数据
            setResult(2, mIntent);
            finish();
        }
    }

    class Datelistener implements DatePickerDialog.OnDateSetListener {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            // TODO Auto-generated method stub
            tv_startTime.setText(year + "-" + (monthOfYear + 1) + "-"
                    + dayOfMonth);
        }
    }

    class Datelistener1 implements DatePickerDialog.OnDateSetListener {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            // TODO Auto-generated method stub
            tv_endTime.setText(year + "-" + (monthOfYear + 1) + "-"
                    + dayOfMonth);
        }
    }

        public int getDateDays(String date1, String date2) {
            date1 = date1.split(" ")[0];
            date2 = date2.split(" ")[0];
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            int days = 0;
            try {
                Date date = sdf.parse(date1);// 通过日期格式的parse()方法将字符串转换成日期
                Date dateBegin = sdf.parse(date2);

                long betweenTime = date.getTime() - dateBegin.getTime();
                days = (int) (betweenTime / 1000 / 60 / 60 / 24);
            } catch (Exception e) {
            }
            return days;
        }


}
